package cn.disicn.util;

import java.util.HashMap;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.google.common.collect.Maps;
import com.jfinal.kit.PropKit;

public class SMSUtil {

	/**
	 * 生成6位数验证码
	 */
	private static Integer getCode() {
		return (int) ((Math.random() * 9 + 1) * 100000);
	}

	/**
	 * 发送短信验证码
	 */
	public static Integer sendSMS(String phone) {
		// 生成随机6位数的验证码
		Integer code = getCode();
		HashMap<String, Object> result = Maps.newHashMap();
		// 初始化SDK
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(PropKit.use("sms.properties").get("RestURL"), PropKit.use("sms.properties").get("port"));
		restAPI.setAccount(PropKit.use("sms.properties").get("sId"), PropKit.use("sms.properties").get("token"));
		restAPI.setAppId(PropKit.use("sms.properties").get("appid"));
		result = restAPI.sendTemplateSMS(phone, PropKit.use("sms.properties").get("mbid"),
				new String[] { code + "",
						PropKit.use("sms.properties").get("time") + "分钟" });
		if ("000000".equals(result.get("statusCode"))) {
			return code;
		} else {
			return null;
		}
	}

}
